/**
 * 贷款申请，获取手机号码的贷款申请
 * @returns 
 */
function chksubmit()
{
	var loanmoney = $('#loanmoney').val();
	var truename = $('#truename').val();
	var source = $('#source').val();
	var city = $('#city').val();
	var mobile = '';
	var phonecode = '';//手机验证码
	var uid = $('#uid').val();
	var loanusertype = $('#loanusertype').val();
	if(loanusertype == 2) {
		alert('亲，您是贷款经理，不需要使用此功能');
		return;
	}
	if(uid == ""){//游客申请状态
		var mobile = $('#mobile').val();
		var uid = $('#uid').val();
		var phonecode = $('#phonecode').val();//手机验证码
		
		var han = /^[\u4e00-\u9fa5]+$/gi;
		var i = /\s/g;
		mobile = mobile.replace(i,'');
		if(!han.test(truename)){
			alert("姓名请输入中文！");
			return;
		}
		
		if(truename == "" || truename == "填写您的姓名") {
			alert('请输入真实姓名');
			return;
		}
		if(truename.length > 4){
			alert("请输入正确的姓名！");
			return;
		}
		if(mobile == "" || mobile == "您的手机号码") {
			alert('请输入手机号码');
			return;
		}
		var tel2 = /^1\d{10}$/;
		if(!tel2.test(mobile))
		{
			alert("手机号码格式不正确");
			return;
		}
		if(city == "") {
			alert('请输入所在城市');
			return;
		}
	}
	mobile = tests(mobile);
	/*************用户协议**********/
	var agereement = $("#agereement").val();
	if(agereement == 0){
		alert('请勾选，《助贷网平台服务协议》');
		return false;
	}
	/*************用户协议**********/
	var json_data = {
		'truename':truename,'mobile':mobile,
		'loanmoney':loanmoney,'city':city,
		'source':source
	};
	$.ajax({
	    type: 'GET',  //这里用GET
	    url: jiekou_site_url+'/index/form/quickapply',
	    dataType: 'jsonp',  //类型
	    data: json_data,
	    jsonp: 'jcallback', //jsonp回调参数，必需
	    async: false,
	    success: function(d) { //返回的json数据
			//1:贷款经理不允许申请2：验证码错误3：重复申请5：其他原因导致的失败 5以上：成功
			if(d == 3) {
				alert('您好，24小时内只允许申请一次');
				return false;
			}else if(d == 5){
				alert('贷款申请失败');
				return false;
			}else if(d == 2){
				alert('验证码错误');
				return false;
			}else if(d == 1){
				alert('亲，您是贷款经理，不需要使用此功能');
				return false;
			}else{
				alert('注册成功');		
			}
	    },
	    timeout: 3000
	});
}

/**
 * 贷款申请，多步骤同时执行
 * @returns 
 */
function loansteps(i)
{
	var loan_id = $('#loan_id').val();
	//第一步
	var sex = getRadioValue("sex");
	var loanmonth = $('#loanmonth').val();
	var usertime = $('#usertime').val();
	var loanuse = $('#loanuse').val();
	var productid = $('#productid').val();
	
	//第二步
	var credit = $('#credit').val();
	var house = getRadioValue("house");
	var house_remark = $('#house_remark').val();
	var baodan = getRadioValue("baodan");
	var baodan_remark = $('#baodan_remark').val();
	var jobid = $('#jobid').val();
	
	if(credit == 0){
		alert("请选择个人信用");
		exit;
	}
	if(i == 1){//无固定职业
		var income_source = $('#income_source').val();
		var monthly = $('#monthly2').val();
		if(income_source == '手写输入'){
			alert('请输入收入来源');
			exit;
		}else if(monthly == 0){
			alert('请选择月均收入！');
			exit;
		}
	}else if(i == 2){//生意人
		var uncompany_card_have = checkvalue('company_card_have');//判断有无营业执照
		var company_card_have = getRadioValue("company_card_have");//获取有无营业执照
		var company_yearincome = $('#company_yearincome').val();
		var company_year_fpmoney = $('#company_year_fpmoney').val();
		if(uncompany_card_have == ''){
			alert('请选择有无营业执照!');
			exit;
		}else if(company_card_have == '有'){ //&& company_card_regarea == '未知'){
			var company_card_regarea = $('#company_card_regarea_h').val();
			var company_card_regyear = $('#company_card_regyear_h').val();
			if(company_card_regarea == '未知'){
				alert('请选择注册地！');
				exit;
			}
			if(company_card_regyear == '未知'){
				alert('请选择注册年限！');
				exit;
			}
		}else if(company_card_have == '无'){ //&& company_card_regarea == '未知'){
			var company_card_regarea = $('#company_card_regarea_n').val();
			var company_card_regyear = $('#company_card_regyear_n').val();
			if(company_card_regarea == '未知'){
				alert('请选择经营地！');
				exit;
			}else if(company_card_regyear == '未知'){
				alert('请选择经营年限！');
				exit;
			}
		}else if(company_yearincome == '未知'){
			alert('请选择年营业收入！');
			exit;
		}else if(company_year_fpmoney == '未知'){
			alert('请选择年开票金额！');
			exit;
		}
	}else if(i == 3){//上班族
		var job_year = $('#job_year').val();
		var monthly_sendtype = $('#monthly_sendtype').val();
		var monthly = $('#monthly').val();
		var fundid = getRadioValue("fundid");
		var unfundid = checkvalue('fundid');
		var company_type = $('#company_type').val();
		var unfundid = getRadioValue("fundid");
		if(job_year == 0){
			alert('请选择工作年限！');
			exit;
		}else if(monthly_sendtype == '未知'){
			alert('请选择工资发放形式！');
			exit;
		}else if(monthly == 0){
			alert('请选择月收入！');
			exit;
		}else if(fundid == ''){
			alert('请选择有无公积金！');
			exit;
		}else if(company_type == 0){
			alert('请选择单位性质！');
			exit;
		}
	}

	var json_data = {
        'loan_id':loan_id, 'sex':sex, 'loanmonth':loanmonth,
		'usertime':usertime, 'loanuse':loanuse, 'productid':productid,
		'credit':credit, 'house':house, 'house_remark':house_remark,
		'baodan':baodan, 'baodan_remark':baodan_remark, 'jobid':jobid,
		'job_year':job_year, 'monthly_sendtype':monthly_sendtype,
		'fundid':fundid, 'company_type':company_type,
		'company_card_have':company_card_have, 'company_card_regarea':company_card_regarea,
		'company_card_regyear':company_card_regyear, 'company_yearincome':company_yearincome,
		'company_year_fpmoney':company_year_fpmoney, 'income_source':income_source,
		'monthly':monthly
	};
	$.ajax({
	    type: 'GET',  //这里用GET
	    url: jiekou_site_url+'/user/loan_steps_apply.html',
	    dataType: 'jsonp',  //类型
	    data: json_data,
	    jsonp: 'jcallback', //jsonp回调参数，必需
	    async: false,
	    success: function(d) { //返回的json数据
			if(d == 2) {
				alert('您好，24小时内只允许申请一次');
				document.getElementById('nmb'+jobid).style.display = 'block';
				return false;
			}else if(d == 3){
				alert('贷款申请失败');
				return false;
			}else if(d == 4){
				alert('验证码错误');
				return false;
			}else if(d == 1){//成功
				$.ajax({
				    type: 'GET',  //这里用GET
				    url: jiekou_site_url+'/user/get_js_login.html',
				    dataType: 'jsonp',  //类型
				    data: {},
				    jsonp: 'jcallback', //jsonp回调参数，必需
				    async: false,
				    success: function(d) { //返回的json数据
						var strs= new Array(); //定义一数组
						strs=d.split("-"); //字符分割 
						if(strs[0]==1){//已经是会员的贷款用户
							document.getElementById('lastshen').style.display = 'block';
						}else{//第一次在平台贷款系统自动帮其注册
							$('#login_name').html(strs[1]);
							$('#login_pwd').html(strs[2]);
							document.getElementById('lastshen2').style.display = 'block';
						}
				    },
				    timeout: 3000
				});
			}
	    },
	    timeout: 3000
	});
}

/**
 * 免费贷款申请
 * @returns {Boolean}
 */
function apply_loan() {	
	var json_data = {};
	//免费贷款申请
	$.ajax({
	    type: 'GET',  //这里用GET
	    url: jiekou_site_url+'/user/checkloan.html',
	    dataType: 'jsonp',  //类型
	    data: json_data,
	    jsonp: 'jcallback', //jsonp回调参数，必需
	    async: false,
	    success: function(d) { //返回的json数据
			if(d == 0) {
				//未登录
				$(".huiyuandl").css('display','block');
				return false;
			}else if(d == 3){//重复申请
				alert('亲，24小时内只允许申请一次');
				return false;
			}else{
				//已登录
				$('.pop-up-box-area').css('display','block');
				var t1 = document.getElementById('nmb1');
				var t2 = document.getElementById('nmb2');
				var t3 = document.getElementById('nmb3');
				var ls = document.getElementById('lastshen');
				var lst = document.getElementById('lastshen2');
				t1.style.display = 'none';
				t2.style.display = 'none';
				t3.style.display = 'none';
				ls.style.display = 'none';
				lst.style.display = 'none';
				return false;
			}
	    },
	    timeout: 3000
	});    
}

/**
 * 发送验证码
 * @returns {Boolean}
 */
function sendcode()
{
	var mobile = $('#mobile').val();
	var msgtype = $('#msgtype').val();//发送短信的类别：1、贷款申请2、注册3、忘记密码
	var loanusertype = $('#loanusertype').val();
	var usertype = $('#usertype').val();
	if(loanusertype == 2) {
		alert('亲，您是贷款经理，不需要使用此功能');
		return;
	}
	if(mobile == "" || mobile == "您的手机号码") {
		alert('请输入手机号码');
		return;
	}
	var tel2 = /^((13[0-9]{9})|(14[0-9]{9})|(15[0-9]{9})|(18[0-9]{9}))$/;
	if(!tel2.test(mobile))
	{
		alert("手机号码格式不正确");
		return;
	}
	document.getElementById("codemsgs").value="正在发送...";
	var json_data = {
		'mobile':mobile,
		'msgtype':msgtype,
		'usertype':usertype
	};
	$.ajax({
	    type: 'GET',  //这里用GET
	    url: jiekou_site_url+'/user/sms_code_loan.html',
	    dataType: 'jsonp',  //类型
	    data: json_data,
	    jsonp: 'jcallback', //jsonp回调参数，必需
	    async: false,
	    success: function(d) { //返回的json数据
			if(d == 1) {
				document.getElementById("codemsgs").value="验证码已发送";
				return;
			}else if(d == 3){
				alert('验证码已发送，三分钟内有效，请失效后再重试！');
				return false;
			}else if(d == 4 || d == 5){
				alert('请勿重复注册');
				return false;
			}else if(d == 6){
				document.getElementById("codemsgs").value="获取短信验证";
				alert('您已经是助贷网会员，请先登录，再申请贷款，更便捷！');
				window.location.href = '/user/login.html';
			}else if(d == 7){
				document.getElementById("codemsgs").value="发送过于频繁";
				alert('亲，验证码3分钟内有效，有效期不允许重复发送');
				return;
			}else{
				document.getElementById("codemsgs").value="验证码发送失败";
				return;
			}
	    },
	    timeout: 3000
	});
}

/**
 * 发送验证码--通用（除注册登陆、贷款外）
 * @returns {Boolean}
 */
function sendcodetong()
{
	var mobile = $('#mobile').val();
	var msgtype = $('#msgtype').val();//发送短信的类别：1、忘记密码 2、验证码登陆
	var randcode = $('#randcode').val();
	
	if(mobile == "" || mobile == "您的手机号码") {
		alert('请输入手机号码');
		return;
	}
	if(randcode == "") {
		alert('请输入图形验证码');
		return;
	}
	var tel2 = /^((13[0-9]{9})|(14[0-9]{9})|(15[0-9]{9})|(18[0-9]{9}))$/;
	if(!tel2.test(mobile))
	{
		alert("手机号码格式不正确");
		return;
	}
	document.getElementById("codemsg").value="正在发送...";
	var json_data = {
		'mobile':mobile,
		'msgtype':msgtype,
		'randcode':randcode
	};
	$.ajax({
	    type: 'GET',  //这里用GET
	    url: jiekou_site_url+'/user/sms_code.html',
	    dataType: 'jsonp',  //类型
	    data: json_data,
	    jsonp: 'jcallback', //jsonp回调参数，必需
	    async: false,
	    success: function(d) { //返回的json数据
			if(d == 1) {
				document.getElementById("codemsg").value="验证码已发送";
				return;
			}else if(d == 7){
				document.getElementById("codemsg").value="发送过于频繁";
				alert('亲，验证码3分钟内有效，有效期不允许重复发送');
				return;
			}else if(d == 8){
				document.getElementById("codemsg").value="图形验证码不正确";
				return;
			}else{
				document.getElementById("codemsg").value="验证码发送失败";
				return;
			}
	    },
	    timeout: 3000
	});
}

function get_randcode(){
	$('#rand_code').attr('src', jiekou_site_url+'/captchacode/verify_image.html?r=' + Math.random());
}

/**
 * 手机验证
 * @returns {Boolean}
 */
function chkmobile(){
	var mobile = $('#mobile').val();
	var reg2 = /^((13[0-9]{9})|(14[0-9]{9})|(15[0-9]{9})|(18[0-9]{9}))$/;
	if(!reg2.test(mobile))
	{
		$('#regmsg').html("<span style='color:red;'>手机号码格式错误</span>");
		return false;
	}else{
		$('#regmsg').html("");
	}
}

//获取radio值
function getRadioValue(radioName) {
    var chkRadio = document.getElementsByName(radioName);
    for (var i = 0; i < chkRadio.length; i++) {
        if (chkRadio[i].checked)
            return chkRadio[i].value;
    }
}

/* 登录页清空功能 */
function cancel()
{
	$('#account').val('');
	$('#password').val('');
	$('#code').val('');
}
/* 登录页验证码刷新 */
function refurbish(){
     var img = document.getElementById("varImg");
     img.src = "inc/code.php?" + Math.random();
} 
/* 菜单列表JS代码 */
function open_close_li(id)
{
	if(document.getElementById(id).style.display == "none") {
		document.getElementById(id).style.display = "block";
	}
	else {
		document.getElementById(id).style.display = "none";
	}
}
function open_close_item(id)
{
	if(document.getElementById(id).className == "openitem") {
		document.getElementById(id).className = "closeitem";
	}
	else {
		document.getElementById(id).className = "openitem";
	}
}
/* 时间代码 */
function reloop()
{
	var time = new Date( ); //获得当前时间
	//获得年、月、日，Date()函数中的月份是从0－11计算
	var year = time.getYear(); 
	var month = time.getMonth()+1;
	var date = time.getDate();
	//获得小时、分钟、秒
	var hour = time.getHours();  
	var minute = time.getMinutes();
	var second = time.getSeconds();
	//获得一个星期中的第几天,西方的星期是从星期日开始，星期六结束
	var day = time.getDay();
	//如果分钟只有1位，补0显示
	if (minute < 10)
	minute="0"+minute;
	//如果秒数只有1位，补0显示
	if (second < 10)
	second="0"+second;
	//按12小时制显示
	if (hour>24) {
		hour=hour-24;
	}
	var weekday = 0;
	//firefox下的getyear是从1900年开始的
	year = (year < 1900)?(1900+year):year;
	
	switch(time.getDay()) {
		case 0:
			weekday = "星期日";
		break;
		case 1:
			weekday = "星期一";
		break;
		case 2:
			weekday = "星期二";
		break;
		case 3:
			weekday = "星期三";
		break;
		case 4:
			weekday = "星期四";
		break;
		case 5:
			weekday = "星期五";
		break;
		case 6:
			weekday = "星期六";
		break;
	}
	
	document.getElementById("t").innerHTML="今天是："+year+"年"+month+"月"+date+"日"+weekday+"  "+hour+"时"+minute+"分"+second+"秒";
	setTimeout("reloop()",1000);
}

function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
    var Days = 180; //此 cookie 将被保存 30 天
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

/**
 * 判断单选是否选中
 * @param rname
 * @returns {Number}
 */
function fn(rname){//定义一个函数 
	   var radio1=document.getElementsByName(rname);
    var flag1 = 0;
	   for(var i=0;i<radio1.length;i++)
	   {
	         if(radio1.item(i).checked==true)
	         {
	             flag1=1;
	             break;
	         }
	   }
	   return flag1;
}

function Counter(nMax,nInterval)
{
	this.maxTime=nMax;
	this.interval=nInterval;
	this.objId="timer";
	this.obj=null;
	this.num=this.maxTime;
	this.timer=null;
	this.start=function()
	{ 
		this.obj=document.getElementById(this.objId);
		if(this.num>0) setTimeout(this.run,this.interval*1000);
	};
	this.run=function()
	{
		if(myCounter.num>0) 
		{
			myCounter.num--;
			myCounter.obj.innerHTML=myCounter.num;
			myCounter.timer=setTimeout(myCounter.run,myCounter.interval*1000);
		}else{
			clearTimeout(myCounter.timer);
		}
	};
	this.show=function()
	{
		document.write("<span id="+this.objId+">"+this.num+"</span>");
		this.obj=document.getElementById(this.objId);
	//alert(this.obj.innerHTML);
	}
}

//判断单选框是否为空
function checkvalue(name){
	 var obj=document.getElementsByName(name);
	 var j = '';
	 //alert(obj.length);
	 for (var i=0;i<obj.length;i++){
		  if(obj[i].checked){
			  j = 1;
			  return j;
		  }
	 }
	 return j;
}
